﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using WpfDemo.Common;

namespace WpfDemo.Converter
{
    public class BoolToVisConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {          
            if(Utils.IsAdmin) return Visibility.Visible;

            // 权限枚举
            //var requiredPermission = (Permission)Enum.Parse(typeof(Permission), parameter.ToString());
            if (value == null) return Visibility.Collapsed;
             
            var path = value.ToString().Replace("WpfDemo.ViewModel.","").Replace("ViewModel", ".Index");
            var auth = Utils.Auths.Where(x=>x.FModuleEntity.F_TargetView.ToLower()== path.ToLower()).ToList();
            if(auth==null||auth.Count()==0) return Visibility.Collapsed;
            var buttoms = auth.Select(m => m.FButtonEntity.F_Code).ToList();
            if(buttoms==null || buttoms.Count()==0) return Visibility.Collapsed;
            
            var temp = parameter.ToString();
            if (buttoms.Contains(temp))
                return Visibility.Visible;

            return Visibility.Collapsed;
           
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if(value==null) return "0";
            var temp = value.ToString();
            if (temp == "True") return "1";
            else return "0";            
        }
    }
}
